package com.baomidou.springwind.pojo;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.springwind.entity.CityOrder;
import com.baomidou.springwind.entity.NetworkNode;
import com.baomidou.springwind.utils.ParamMap;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.math.BigDecimal;
import java.net.URLEncoder;
import java.util.*;

public class CityPeopleGroup {
    public List<LinkedHashMap<String, Object>> MaxHashMap(HashMap<String, CityOrder> Hm1, String xy, double workerdaytime, double timeRecovery) {
        //对前台输入的数据进行处理
        //人数
        double iman=0;
        //建立一个HashMap,key为坐标的名称，value为业务量
        HashMap<String, BigDecimal> YeWuLiang = YeWuShu(Hm1);
        //只有一个点的情况
        HashMap<String, Double> mapPath1 = new HashMap<String, Double>();
        //只有2个点的情况
        HashMap<String, Double> mapPath2 = new HashMap<String, Double>();
        //只有3个点的情况
        HashMap<String, Double> mapPath3 = new HashMap<String, Double>();
        //确立返回值
        List<LinkedHashMap<String, Object>> ret = new ArrayList<LinkedHashMap<String, Object>>();
        //开始循环地址
        while (Hm1.size() > 0) {
            LinkedHashMap<String, Object> map1 = new LinkedHashMap<String,Object>();
            HashMap<String, CityOrder> Hms = (HashMap<String, CityOrder>) Hm1.clone();
            //对象复制

            //找到横坐标最大一个
            String MaxName = maxLngLat(Hms,xy);
            BigDecimal zuobiaoY = BigDecimal.valueOf(Double.valueOf(Hms.get(MaxName).getLat1()));
            BigDecimal zuobiaoX = BigDecimal.valueOf(Double.valueOf(Hms.get(MaxName).getLng1()));

            //除去最大坐标，剩余的坐标
            HashMap<String, CityOrder> Hm2 =
                    hashmap2(Hms, MaxName);
            int size1 = Hm1.size();
            //业务量
            Double MaxYeWuLiang = YeWuLiang.get(MaxName).doubleValue();
            //第一个位置的坐标处理
            StringBuilder originsParam = new StringBuilder();
            originsParam.append(zuobiaoX.setScale(3, BigDecimal.ROUND_HALF_UP));
            originsParam.append(",");
            originsParam.append(zuobiaoY.setScale(3, BigDecimal.ROUND_HALF_UP));
            //连接第一个点到其他点之间的距离
            HashMap<String, Double> hash3 = new HashMap<String, Double>();


            for (String key : Hm2.keySet()) {
                //第二个位置的坐标处理
                StringBuilder destinationsParam = new StringBuilder();
                destinationsParam.append(Hm2.get(key).getLng1());
                destinationsParam.append(",");
                destinationsParam.append(Hm2.get(key).getLat1());
                //调用api,获取到第一到第二的距离
                //  String juli = dz.TwoJuLi(originsParam, destinationsParam);
                //判断距离是否超过10公里，是的话，就删除掉
                //   String juli1 = juli.substring(0, juli.length() - 2);
                try {
                    Double juli2 = Node1ToNode2Distance(zuobiaoX.setScale(3, BigDecimal.ROUND_HALF_UP).doubleValue(),zuobiaoY.setScale(3, BigDecimal.ROUND_HALF_UP).doubleValue(),Double.valueOf(Hm2.get(key).getLng1()),Double.valueOf(Hm2.get(key).getLat1()));
                    //Double.valueOf(juli1);
                    //记录下第一个点到第二个点的距离
                    if (juli2 < 5) {
                        hash3.put(MaxName + "-" + Hm2.get(key).getName(), juli2);
                    }

                } catch (NumberFormatException e) {
                    e.printStackTrace();
                }
            }
            //记录第二个点到第3个点
            HashMap<String, Double> hash4 = new HashMap<String, Double>();
            //开始从第二个点找到第三点了
            for (String key : hash3.keySet()) {
                HashMap<String, CityOrder> Hm3 = (HashMap<String, CityOrder>) Hm2.clone();
                String keys = key.split("-")[1];

                //求第2个点到第三个点的距离
                BigDecimal zuobiaoY1 = BigDecimal.valueOf(Double.valueOf(Hm3.get(keys).getLat1()));
                BigDecimal zuobiaoX1 = BigDecimal.valueOf(Double.valueOf(Hm3.get(keys).getLng1()));
                StringBuilder originsParam1 = new StringBuilder();
                originsParam1.append(zuobiaoX1.setScale(2, BigDecimal.ROUND_HALF_UP));
                originsParam1.append(",");
                originsParam1.append(zuobiaoY1.setScale(2, BigDecimal.ROUND_HALF_UP));
                //Hm3是排除了第一个点和第二点的坐标
                Hm3.remove(keys);
                Hm3.remove(MaxName);
                for (String key2 : Hm3.keySet()) {
                    //第3个点的坐标处理
                    StringBuilder destinationsParam1 = new StringBuilder();
                    destinationsParam1.append(Hm3.get(key2).getLng1());
                    destinationsParam1.append(",");
                    destinationsParam1.append(Hm3.get(key2).getLat1());
                    //获取到距离
                    // String juli = dz.TwoJuLi(originsParam1, destinationsParam1);
                    //判断距离是否超过5公里，是的话，就删除掉
                    // String juli3 = juli.substring(0, juli.length() - 2);
                    try {
                        Double juli4 = Node1ToNode2Distance(zuobiaoX1.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue(),zuobiaoY1.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue(),Double.valueOf(Hm3.get(key2).getLng1()),Double.valueOf(Hm3.get(key2).getLat1()));
                        //Double.valueOf(juli3);

                        //记录下第二个点到第三个点的距离
                        if (juli4 < 5) {
                            hash4.put(keys + "-" + key2, juli4);
                        }
                    } catch (NumberFormatException e) {
                        e.printStackTrace();
                    }
                }
            }
            //当周围没有一个可到的对象的时候
            if (hash3.size() == 0) {
                mapPath1.put(MaxName, YeWuLiang.get(MaxName).doubleValue());
                Hm1.remove(MaxName);
                double man=Math.ceil(YeWuLiang.get(MaxName).doubleValue()/16500*1.3/workerdaytime);
                iman=iman+man;
                map1.put("nodeName1",MaxName);
                map1.put("nodeName1Number",YeWuLiang.get(MaxName).doubleValue());
                map1.put("nodeName2","");
                map1.put("nodeName2Number",0);
                map1.put("nodeName3","");
                map1.put("nodeName3Number",0);
                map1.put("shelf",Math.ceil(YeWuLiang.get(MaxName).doubleValue()/550/30*1.3));
                map1.put("distribute", Math.ceil(YeWuLiang.get(MaxName).doubleValue()/550/30*1.3/workerdaytime));
                map1.put("recovery", Math.ceil(YeWuLiang.get(MaxName).doubleValue()/550/30*1.3/timeRecovery));
                ret.add(map1);

            }
            //当有第3个点的情况
            if (hash4.size() > 0) {
                //组合 第一个地点--距离--第二个地点--距离---第三个地点
                String array[][] = new String[hash3.size() * hash4.size()][5];
                int i1 = 0;
                for (String key : hash3.keySet()) {
                    for (String key2 : hash4.keySet()) {
                        if (key.split("-")[1].equals(key2.split("-")[0])) {
                            array[i1][0] = key.split("-")[0];
                            array[i1][1] = hash3.get(key).toString();
                            array[i1][2] = key.split("-")[1];
                            array[i1][3] = hash4.get(key2).toString();
                            array[i1][4] = key2.split("-")[1];
                            i1++;
                        }
                    }
                }
                Map<Integer, Double> mapfancha = new LinkedHashMap<Integer, Double>();

                for (int i4 = 0; i4 < i1; i4++) {
                    String str1 = array[i4][0];
                    //通过地点名称获取到对应的map值，并找到对应的库存量
                    Double double1 = YeWuLiang.get(array[i4][0]).doubleValue();
                    Double double2 = YeWuLiang.get(array[i4][2]).doubleValue();
                    Double double3 = YeWuLiang.get(array[i4][4]).doubleValue();
                    Double double4 = variance3(double1, double2, double3);
                    mapfancha.put(i4, double4);
                }
                //获取到一个标准差为基准
                double fangcha = 1;
                for (int key : mapfancha.keySet()) {
                    fangcha = mapfancha.get(key);
                    break;
                }
                int keyfangcha = 0;
                //找到方差最小的那个
                for (int key : mapfancha.keySet()) {
                    if (mapfancha.get(key) <= fangcha) {
                        fangcha = mapfancha.get(key);
                        keyfangcha = key;
                    }
                }

                //找到3个点钟业务量最大一个点
                Double double1 = YeWuLiang.get(array[keyfangcha][0]).doubleValue();
                Double double2 = YeWuLiang.get(array[keyfangcha][2]).doubleValue();
                Double double3 = YeWuLiang.get(array[keyfangcha][4]).doubleValue();
                Double YeWuLiang3Max = double1+double2+double3;
                //dz.fangchaMax(double1, double2, double3);
                double man=Math.ceil(YeWuLiang3Max/16500*1.3/workerdaytime);
                iman=iman+man;

                mapPath3.put(array[keyfangcha][0] + array[keyfangcha][2] + array[keyfangcha][4], YeWuLiang3Max);
                map1.put("nodeName1",array[keyfangcha][0]);
                map1.put("nodeName1Number",double1);
                map1.put("nodeName2",array[keyfangcha][2]);
                map1.put("nodeName2Number",double2);
                map1.put("nodeName3",array[keyfangcha][4]);
                map1.put("nodeName3Number",double3);
                map1.put("shelf",Math.ceil(YeWuLiang3Max/550/30*1.3));
                map1.put("distribute", Math.ceil(YeWuLiang3Max/550/30*1.3/workerdaytime));
                map1.put("recovery", Math.ceil(YeWuLiang3Max/550/30*1.3/timeRecovery));
                ret.add(map1);
                //去除掉3个坐标的数据
                Hm1.remove(array[keyfangcha][0]);
                Hm1.remove(array[keyfangcha][2]);
                Hm1.remove(array[keyfangcha][4]);
            }
            //当只有2个点一起情况

            if (hash4.size() == 0 && hash3.size() != 0) {

                //找到差值绝对值
                Double double1 = YeWuLiang.get(MaxName).doubleValue();
                Double doublefangcha;
                HashMap<String, Double> hasmapfangcha = new HashMap<String, Double>();
                for (String key : hash3.keySet()) {
                    Double double2 = YeWuLiang.get(key.split("-")[1]).doubleValue();
                    doublefangcha = variance2(double1, double2);
                    hasmapfangcha.put(key.split("-")[1], doublefangcha);
                }
                //获取到一个标准差为基准
                Double fangcha = 1.0;
                for (String key : hasmapfangcha.keySet()) {
                    fangcha = hasmapfangcha.get(key);
                    break;
                }
                String keyfangcha = null;
                //找到方差最小的那个
                for (String key : hasmapfangcha.keySet()) {
                    if (hasmapfangcha.get(key) <= fangcha) {
                        fangcha = hasmapfangcha.get(key);
                        keyfangcha = key;
                    }
                }
                Double YeWuLiang2Max = MaxYeWuLiang+YeWuLiang.get(keyfangcha).doubleValue();
                //dz.fangchaMax2(MaxYeWuLiang, YeWuLiang.get(keyfangcha).doubleValue());
                double man=Math.ceil(YeWuLiang2Max/16500*1.3/workerdaytime);
                iman=iman+man;
                //保存2个点的数据
                mapPath2.put(MaxName + keyfangcha, YeWuLiang2Max);
                map1.put("nodeName1",MaxName);
                map1.put("nodeName1Number",MaxYeWuLiang);
                map1.put("nodeName2",keyfangcha);
                map1.put("nodeName2Number",YeWuLiang.get(keyfangcha).doubleValue());
                map1.put("nodeName3","");
                map1.put("nodeName3Number",0);
                map1.put("shelf",Math.ceil(YeWuLiang2Max/550/30*1.3));
                map1.put("distribute", Math.ceil(YeWuLiang2Max/550/30*1.3/workerdaytime));
                map1.put("recovery", Math.ceil(YeWuLiang2Max/550/30*1.3/timeRecovery));
                ret.add(map1);
                Hm1.remove(MaxName);
                Hm1.remove(keyfangcha);
            }
        }

        return ret;
    }
    /*
    算出分组结果
     */
    public LinkedHashMap<String,CityDrivingRoute> orderHashMap(HashMap<String, CityOrder> orderMap,ParamMap<String, Object> pm) {
        //申明变量
        int distanceOne=Integer.valueOf(pm.get("distance1").toString());
        int distanceTwo=Integer.valueOf(pm.get("distance2").toString());
        double distance1=100;
        double distance2=100;
        CityOrder cityOrder1;
        CityOrder cityOrder2;
        CityOrder cityOrder3;
        int i23=0;
        int is=3;
        double variance=0;
        int variance_i=0;
        List<CityDrivingRoute> drivingRouteList;
        CityDrivingRoute cityDrivingRoute;
        LinkedHashMap<String,List<CityDrivingRoute>> cityDrivingRoutes1 = new LinkedHashMap<String, List<CityDrivingRoute>>();
        LinkedHashMap<String,List<CityDrivingRoute>> cityDrivingRoutes2 = new LinkedHashMap<String, List<CityDrivingRoute>>();
        LinkedHashMap<String,List<CityDrivingRoute>> cityDrivingRoutes3 = new LinkedHashMap<String, List<CityDrivingRoute>>();
        LinkedHashMap<String,CityDrivingRoute> cityResult = new LinkedHashMap<String, CityDrivingRoute>();

        //所有的情况
        for (HashMap.Entry entry1:orderMap.entrySet()){
            //第一个点
            cityOrder1=(CityOrder) entry1.getValue();
            drivingRouteList=new ArrayList<CityDrivingRoute>();
            //只有1个点的情况
            cityDrivingRoute=new CityDrivingRoute(cityOrder1,0,null,0,null);
            drivingRouteList.add(cityDrivingRoute);
            cityDrivingRoutes1.put(entry1.getKey().toString(),drivingRouteList);
            //只有2个点的情况
            List<CityDrivingRoute> drivingRouteList2=new ArrayList<CityDrivingRoute>();
            for (HashMap.Entry entry2:orderMap.entrySet()){
                //第二个点
                cityOrder2=(CityOrder) entry2.getValue();
                if(entry1.getKey()!=entry2.getKey()){
                    distance1=Node1ToNode2Distance(Double.valueOf(cityOrder1.getLng1()),Double.valueOf(cityOrder1.getLat1()),Double.valueOf(cityOrder2.getLng1()),Double.valueOf(cityOrder2.getLat1()));
                    if(distance1<distanceOne){
                        cityDrivingRoute=new CityDrivingRoute(cityOrder1,distance1,cityOrder2,0,null);
                        drivingRouteList2.add(cityDrivingRoute);
                        List<CityDrivingRoute> drivingRouteList3=new ArrayList<CityDrivingRoute>();
                        for(HashMap.Entry entry3:orderMap.entrySet()){
                            //第3个点
                            cityOrder3=(CityOrder) entry3.getValue();
                            if(entry1.getKey()!=entry2.getKey()&&entry2.getKey()!=entry3.getKey()&&entry1.getKey()!=entry3.getKey()){
                                distance2=Node1ToNode2Distance(Double.valueOf(cityOrder2.getLng1()),Double.valueOf(cityOrder2.getLat1()),Double.valueOf(cityOrder3.getLng1()),Double.valueOf(cityOrder3.getLat1()));
                                if(distance2<distanceTwo){
                                    cityDrivingRoute=new CityDrivingRoute(cityOrder1,distance1,cityOrder2,distance2,cityOrder3);
                                    drivingRouteList3.add(cityDrivingRoute);
                                }
                            }
                        }
                        if(drivingRouteList3.size()>0){
                            cityDrivingRoutes3.put(entry1.getKey().toString(),drivingRouteList3);
                        }
                    }
                }
            }
            if(drivingRouteList2.size()>0){
                cityDrivingRoutes2.put(entry1.getKey().toString(),drivingRouteList2);
            }
        }
        //开始循环
        while(orderMap.size()>0){
            String maxlat=maxLngLat(orderMap,pm.get("lnglat").toString());
            if(maxlat==null){
                break;
            }
            //当有3个点的情况
            drivingRouteList=cityDrivingRoutes3.get(maxlat);
            if (drivingRouteList!=null){
                List<Double> varianceList = new LinkedList<Double>();
                for (int i=0;i<drivingRouteList.size();i++){
                    varianceList.add(variance3(drivingRouteList.get(i).node1.getOrderPositive(),drivingRouteList.get(i).node2.getOrderPositive(),drivingRouteList.get(i).node3.getOrderPositive()));
                }
                variance=varianceList.get(0);
                for (int i=0;i<varianceList.size();i++){
                    if(varianceList.get(i)<=variance){
                        variance=varianceList.get(i);
                        variance_i=i;
                    }
                }
                cityResult.put(maxlat,drivingRouteList.get(variance_i));
                orderMap.remove(drivingRouteList.get(variance_i).node1.getName());
                orderMap.remove(drivingRouteList.get(variance_i).node2.getName());
                orderMap.remove(drivingRouteList.get(variance_i).node3.getName());
                cityDrivingRoutes1.remove(drivingRouteList.get(variance_i).node1.getName());
                cityDrivingRoutes2.remove(drivingRouteList.get(variance_i).node1.getName());
                cityDrivingRoutes3.remove(drivingRouteList.get(variance_i).node1.getName());
                cityDrivingRoutes1.remove(drivingRouteList.get(variance_i).node2.getName());
                cityDrivingRoutes2.remove(drivingRouteList.get(variance_i).node2.getName());
                cityDrivingRoutes3.remove(drivingRouteList.get(variance_i).node2.getName());
                cityDrivingRoutes1.remove(drivingRouteList.get(variance_i).node3.getName());
                cityDrivingRoutes2.remove(drivingRouteList.get(variance_i).node3.getName());
                cityDrivingRoutes3.remove(drivingRouteList.get(variance_i).node3.getName());
                deleteNodes(cityDrivingRoutes1,cityDrivingRoutes2,cityDrivingRoutes3,drivingRouteList.get(variance_i));
                continue;
            }
            //当有2个点的情况
            drivingRouteList=cityDrivingRoutes2.get(maxlat);
            if(drivingRouteList!=null){
                List<Double> varianceList = new LinkedList<Double>();
                for (int i=0;i<drivingRouteList.size();i++){
                    varianceList.add(variance2(drivingRouteList.get(i).node1.getOrderPositive(),drivingRouteList.get(i).node2.getOrderPositive()));
                }
                variance=varianceList.get(0);
                for (int i=0;i<varianceList.size();i++){
                    if(varianceList.get(i)<=variance){
                        variance=varianceList.get(i);
                        variance_i=i;
                    }
                }
                cityResult.put(maxlat,drivingRouteList.get(variance_i));
                orderMap.remove(drivingRouteList.get(variance_i).node1.getName());
                orderMap.remove(drivingRouteList.get(variance_i).node2.getName());
                cityDrivingRoutes1.remove(drivingRouteList.get(variance_i).node1.getName());
                cityDrivingRoutes2.remove(drivingRouteList.get(variance_i).node1.getName());
                cityDrivingRoutes3.remove(drivingRouteList.get(variance_i).node1.getName());
                cityDrivingRoutes1.remove(drivingRouteList.get(variance_i).node2.getName());
                cityDrivingRoutes2.remove(drivingRouteList.get(variance_i).node2.getName());
                cityDrivingRoutes3.remove(drivingRouteList.get(variance_i).node2.getName());
                deleteNodes(cityDrivingRoutes1,cityDrivingRoutes2,cityDrivingRoutes3,drivingRouteList.get(variance_i));
                continue;
            }
            //当有1个点的情况
            drivingRouteList=cityDrivingRoutes1.get(maxlat);
            if(drivingRouteList!=null){
                cityResult.put(maxlat,drivingRouteList.get(0));
                orderMap.remove(drivingRouteList.get(0).node1.getName());
                cityDrivingRoutes1.remove(drivingRouteList.get(0).node1.getName());
                cityDrivingRoutes2.remove(drivingRouteList.get(0).node1.getName());
                cityDrivingRoutes3.remove(drivingRouteList.get(0).node1.getName());
                deleteNodes(cityDrivingRoutes1,cityDrivingRoutes2,cityDrivingRoutes3,drivingRouteList.get(0));
                continue;
            }
        }
        return cityResult;

    }
    public List<LinkedHashMap<String, Object>> orderCombination(LinkedHashMap<String,CityDrivingRoute> cityDrivingRoute,ParamMap<String, Object> pm){

        double hours=60;
        double objectWorkTime=Double.valueOf(pm.get("dayTime").toString());
        double UniteliveryTime=hours/Double.valueOf(pm.get("cost1").toString());
        double workerdaytime=objectWorkTime*UniteliveryTime;
        double recovery=hours/Double.valueOf(pm.get("cost2").toString());
        double timeRecovery=objectWorkTime*recovery;
        int months=Integer.valueOf(pm.get("parameter").toString());
        int allday=Integer.valueOf(pm.get("days").toString());
        double coefficient1=Double.valueOf(pm.get("coefficient").toString());

        List<LinkedHashMap<String, Object>> hashMapList=new ArrayList<LinkedHashMap<String, Object>>();
        for (HashMap.Entry<String,CityDrivingRoute> hashmap:cityDrivingRoute.entrySet()){
            LinkedHashMap<String, Object> map1 = new LinkedHashMap<String,Object>();
            double orderSum=hashmap.getValue().node1.getOrderPositive();
            map1.put("nodeName1",hashmap.getValue().node1.getName());
            map1.put("nodeName1Number",hashmap.getValue().node1.getOrderPositive());
            map1.put("nodeName2","");
            map1.put("nodeName2Number",0);
            if(hashmap.getValue().node2!=null){
                map1.put("nodeName2",hashmap.getValue().node2.getName());
                map1.put("nodeName2Number",hashmap.getValue().node2.getOrderPositive());
                orderSum=orderSum+hashmap.getValue().node2.getOrderPositive();
            }
            map1.put("nodeName3","");
            map1.put("nodeName3Number",0);
            if (hashmap.getValue().node3!=null){
                map1.put("nodeName3",hashmap.getValue().node3.getName());
                map1.put("nodeName3Number",hashmap.getValue().node3.getOrderPositive());
                orderSum=orderSum+hashmap.getValue().node3.getOrderPositive();
            }
            map1.put("shelf",Math.ceil(orderSum/allday/months*coefficient1));
            map1.put("distribute", Math.ceil(orderSum/allday/months*coefficient1/workerdaytime));
            map1.put("recovery", Math.ceil(orderSum/allday/months*coefficient1/timeRecovery));
            hashMapList.add(map1);
        }
        return hashMapList;
    }
    public void deleteNodes(LinkedHashMap<String,List<CityDrivingRoute>> cityDrivingRoutes1,LinkedHashMap<String,List<CityDrivingRoute>> cityDrivingRoutes2,LinkedHashMap<String,List<CityDrivingRoute>> cityDrivingRoutes3,CityDrivingRoute cityDrivingRoute){
        String node1=cityDrivingRoute.node1.getName();
        String node2=null;
        if(cityDrivingRoute.node2!=null){
            node2=cityDrivingRoute.node2.getName();
        }
        String node3=null;
        if(cityDrivingRoute.node3!=null){
            node3=cityDrivingRoute.node3.getName();
        }
        deleteNodes1(cityDrivingRoutes1,node1);
        deleteNodes1(cityDrivingRoutes2,node1);
        deleteNodes1(cityDrivingRoutes3,node1);
        if(node2!=null){
            deleteNodes1(cityDrivingRoutes1,node2);
            deleteNodes1(cityDrivingRoutes2,node2);
            deleteNodes1(cityDrivingRoutes3,node2);
        }
        if(node3!=null){
            deleteNodes1(cityDrivingRoutes1,node3);
            deleteNodes1(cityDrivingRoutes2,node3);
            deleteNodes1(cityDrivingRoutes3,node3);
        }
    }
    public void deleteNodes1(LinkedHashMap<String,List<CityDrivingRoute>> cityDrivingRoutes,String node){
        for(HashMap.Entry map:cityDrivingRoutes.entrySet()){
            List<CityDrivingRoute> list=(List<CityDrivingRoute>) map.getValue();
            Iterator it=list.iterator();
            while (it.hasNext()){
                CityDrivingRoute cityDrivingRoute=(CityDrivingRoute)it.next();
                if(node.equals(cityDrivingRoute.node1.getName())){
                    it.remove();
                }
                if(cityDrivingRoute.node2!=null){
                    if(node.equals(cityDrivingRoute.node2.getName())){
                        it.remove();
                    }
                }
                if(cityDrivingRoute.node3!=null){
                    if(node.equals(cityDrivingRoute.node3.getName())){
                        it.remove();
                    }
                }
            }
        }
        for(Iterator<Map.Entry<String,List<CityDrivingRoute>>> it=cityDrivingRoutes.entrySet().iterator();it.hasNext();){
            Map.Entry<String,List<CityDrivingRoute>> item=it.next();
            if (item.getValue().size()==0){
                it.remove();
            }
        }

    }
    //找出坐标位置最大一个值
    public  String maxLngLat(HashMap<String,CityOrder> Hm1, String xy){
        String MaxName=null;
        BigDecimal zuobiaoY=new BigDecimal(0);
        BigDecimal zuobiaoX=new BigDecimal(0);
        if("1".equals(xy)){
            for (String key : Hm1.keySet()) {
                if(BigDecimal.valueOf(Double.valueOf(Hm1.get(key).getLat1())).compareTo(zuobiaoY)==1){
                    zuobiaoY=BigDecimal.valueOf(Double.valueOf(Hm1.get(key).getLat1()));
                    MaxName=Hm1.get(key).getName();
                }
            }
        }
        if("2".equals(xy)){
            for (String key : Hm1.keySet()) {
                if(BigDecimal.valueOf(Double.valueOf(Hm1.get(key).getLat1())).compareTo(zuobiaoY)==0){
                    zuobiaoY=BigDecimal.valueOf(Double.valueOf(Hm1.get(key).getLat1()));
                    MaxName=Hm1.get(key).getName();
                }
            }
        }
        if("3".equals(xy)){
            for (String key : Hm1.keySet()) {
                if(BigDecimal.valueOf(Double.valueOf(Hm1.get(key).getLng1())).compareTo(zuobiaoX)==1){
                    zuobiaoX=BigDecimal.valueOf(Double.valueOf(Hm1.get(key).getLng1()));
                    MaxName=Hm1.get(key).getName();
                }
            }
        }
        if("4".equals(xy)){
            for (String key : Hm1.keySet()) {
                if(BigDecimal.valueOf(Double.valueOf(Hm1.get(key).getLng1())).compareTo(zuobiaoX)==0){
                    zuobiaoX=BigDecimal.valueOf(Double.valueOf(Hm1.get(key).getLng1()));
                    MaxName=Hm1.get(key).getName();
                }
            }
        }
        return MaxName;
    }
    //调用百度api接口，返回距离
    public  String TwoJuLi(StringBuilder originsParam,StringBuilder destinationsParam){
        //调用百度地图api
        List<JSONObject> result = new ArrayList<JSONObject>();
        //   destinationsParam.deleteCharAt(destinationsParam.lastIndexOf("|"));
        String url = String.format("http://api.map.baidu.com/routematrix/v2/driving?output=json&origins=%s&destinations=%s&ak=%s", URLEncoder.encode(originsParam.toString()), URLEncoder.encode(destinationsParam.toString()), "NURx5GUAQ1IBZVBMCQSpYppRsDdTpbxG");
        String Juli=null;
        CloseableHttpClient client = HttpClients.createDefault();
        try {
            HttpGet httpGet = new HttpGet(url);
            HttpResponse httpResponse = client.execute(httpGet);
            if (httpResponse.getStatusLine().getStatusCode() == 200) {
                HttpEntity httpEntity = httpResponse.getEntity();
                String text = EntityUtils.toString(httpEntity);//取出应答字符串
                JSONObject json = JSONObject.parseObject(text);

                String i1=null;
                JSONArray array = json.getJSONArray("result");

                JSONObject distance = array.getJSONObject(0);
                //   distance.put("node", destinations.get(i));
                //   result.add(distance);

                Juli=distance.getJSONObject("distance").getString("text");


            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Juli;

    }

    //重新建立一个数组
    public  HashMap<String,CityOrder> hashmap2(HashMap<String,CityOrder> Hm1,String MaxName){
        Hm1.remove(MaxName);
        return Hm1;
    }

    //求3个数的方差
    public  double variance3(double a,double b,double c) {
        double sum=0;
        sum=a+b+c;
        double dAve=sum/3;//求平均值
        double dVar=0;
        //求方差
        dVar+=(a-dAve)*(a-dAve);
        dVar+=(b-dAve)*(b-dAve);
        dVar+=(c-dAve)*(c-dAve);
        return dVar/3;
    }
    //求2个数的方差
    public  double variance2(double a,double b) {
        double sum=0;
        sum=a+b;
        double dAve=sum/2;//求平均值
        double dVar=0;
        //求方差
        dVar+=(a-dAve)*(a-dAve);
        dVar+=(b-dAve)*(b-dAve);
        return dVar/2;
    }
    //建立一个HashMap,key为坐标的名称，value为业务量
    public  HashMap<String,BigDecimal> YeWuShu(HashMap<String,CityOrder> Hm1){
        HashMap<String,BigDecimal> YeWuLiang = new HashMap<String,BigDecimal>();
        for (String key : Hm1.keySet()) {
            YeWuLiang.put(Hm1.get(key).getName(),BigDecimal.valueOf(Hm1.get(key).getOrderPositive()));
        }
        return YeWuLiang;
    }
    //找到3个数中最大一个
    public  double fangchaMax(double a,double b,double c) {
        Double Max=a;
        if(a<b){
            Max=b;
        }
        if(Max<c){
            Max=c;
        }
        return Max;
    }
    //找到2个数中最大一个
    public  double fangchaMax2(double a,double b) {
        Double Max=a;
        if(a<b){
            Max=b;
        }
        return Max;
    }
    //根据2个点的经纬度算出他们的直线距离
    //public  double Node1ToNode2Distance(double wd1,double jd1,double wd2,double jd2)
    public  double Node1ToNode2Distance(double jd1,double wd1,double jd2,double wd2)
    {
        double x,y,out;
        double PI=3.14159265;
        double R=6.371229*1e6;

        //  x=(jd2-jd1)*PI*R*Math.cos( ((wd1+wd2)/2) *PI/180)/180;
        //  y=(wd2-wd1)*PI*R/180;
        x=(wd2-wd1)*PI*R*Math.cos( ((wd1+wd2)/2) *PI/180)/180;
        y=(jd2-jd1)*PI*R/180;
        out=Math.hypot(x,y);
        return out/1000;
    }
}
